package com.ddlottery.analytic;

import com.ddlottery.dao.DDuserMapper;
import com.ddlottery.model.DDorder;
import com.ddlottery.model.DDuser;
import com.ddlottery.tools.tools;
import com.whatever.lottery.jczq.Bet;
import com.whatever.omr.OmrText;
import com.whatever.slip.jczq.SlipJCZQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ElNino on 16/4/11.
 */
@Repository
public class jczq {

    @Autowired
    DDuserMapper DDuserMapper;

    public DDorder getInfo(DDorder info , List matchList,Integer machType){
        String chuan = info.getStr().split("\\|")[info.getStr().split("\\|").length-1];
        String play = info.getStr().split("\\|")[1];
        String bei = info.getStr().split("\\|")[0];
        String money = info.getMoney().toString();
        String userStr = "游客";
        if(info.getUid()!=-1) {
            DDuser user = DDuserMapper.selectByPrimaryKey(info.getUid());
            userStr = user.getMobile().substring(0, 3)+"*****"+user.getMobile().substring(7, 11);
        }
        List<String> matchinfo =getBetStrArr(info.getStr(),matchList);
        info.getClosetime().setTime(info.getClosetime().getTime() - 1000 * 60 * 30);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String closetime=sdf.format(info.getClosetime());
        SimpleDateFormat cd=new SimpleDateFormat("yyyyMMdd");
        String oid =info.getOrdernum();
        String str=getPrintLotCode(info.getStr(),closetime,chuan,bei,money,userStr,play,matchinfo,oid,machType);
        info.setStr(str);
        return info;
    }

    public  List getMatchList(ArrayList<DDorder> list) throws IOException {
        Set<String> set = new HashSet<String>();
        for (DDorder info : list) {
            String[] match = info.getContent().split(",");
            set.addAll(Arrays.asList(match));
        }
        String matchs = set.toString().replace("[","").replace("]","").replace(" ","");
        tools tools = new tools();
        List matchList = tools.getMatch(matchs);
        return matchList;
    }

    public String getPrintLotCode(
            String betStr,
            String closetime,
            String chuan,
            String bei,
            String money,
            String username,
            String play,List<String> list,String oid,Integer machType) {
        try {
            betStr = betStr.replaceAll("0503","0508");
            Bet bet = new Bet(betStr);
            String str = SlipJCZQ.parse(bet,machType);
            OmrText t = new OmrText();
            t.appendH1Centerln("彩票投注单");
            if(username == "游客"){
                t.appendCenterln("订单号:"+oid);
            }else{
                t.appendCenterln("用户:"+username);
            }
            String type = "";
            if(play.equals("HH")){
                type ="混合";
            }else if(play.equals("SPF")){
                type ="胜平负";
            }else if(play.equals("RQSPF")){
                type ="让球胜平负";
            }else if(play.equals("BQC")){
                type ="半全场";
            }else if(play.equals("JQS")){
                type ="进球数";
            }else if(play.equals("CBF")){
                type ="比分";
            }
            String bh = "";
            if(machType == 1) {
                bh = " 倍数"+bei+" 合计"+money;

            }
            if(chuan.equals("1*1")){
                t.appendCenterln("竞彩足球单关固定"+bh);
            }else {
                t.appendCenterln("竞彩足球" + type + "过关  " + chuan.replace("*","串")+bh);
            }

            t.appendLineln();
            if(machType == 0) {
                for (String li : list) {
                    t.appendLeftln(li);
                }
                t.appendLeftln("倍数"+bei+" 合计"+money+" 截止时间"+closetime);
                t.appendLeftln("");
                t.appendLeftln("");
                t.appendLineln();
                t.appendLeftln("");
                t.appendLeftln("");
            }
            t.appendHex(str);
            t.appendLineln();
            t.appendH1Centerln("请核对投注内容及截止时间");
            t.appendCenterln("最终解释权归苋菜网所有");
            t.appendCenterln("客服电话：021-61538959");
            t.appendFeed();
            t.appendCut();
            return t.toString();
        }catch (Exception e){
            return "";
        }
    }

    public  List<String> getBetStrArr(String betStr,List matchList){

        List<String> list = new ArrayList();
        String[] str = betStr.split("\\|");
        String play = str[1];
        String bet = str[2];
        String chuan = str[str.length-1];
        String[] betArr = bet.split(",");
        if(!play.equals("HH")){
            for(int i=0;i<betArr.length;i++){
                String mat = betArr[i].split("=")[0];
                String sel = betArr[i].split("=")[1];
                for(int j=0;j<matchList.size();j++){
                    Map map =(Map)matchList.get(j);
                    if(mat.equals(map.get("itemid").toString())){
                        String itemname = map.get("itemname").toString();
                        String host = map.get("host").toString();
                        String visit = map.get("visit").toString();
                        String lose = map.get("lose").toString();
                        Integer guan = i+1;
                        String oneC = itemname ;
                        if(!chuan.equals("1*1")) {
                            oneC = "第" + guan + "关 " + oneC;
                        }
                        if(play.equals("SPF")){
                            oneC = oneC +" 胜平负";
                        }else if(play.equals("RQSPF")){
                            oneC = oneC +" 让球胜平负 让球"+lose;
                        }else if(play.equals("JQS")){
                            oneC = oneC +" 进球数";
                        }else if(play.equals("BQC")) {
                            oneC = oneC + "半全场";
                        } else if(play.equals("CBF")){
                            oneC = oneC +"比分";
                        }
                        list.add(oneC);
                        list.add("主队："+host+" VS 客队："+visit);
                        list.add(tz(play,sel));
                    }
                }
            }
        }else{
            for(int i=0;i<betArr.length;i++){
                String[] arr = betArr[i].split("\\>");
                play = arr[0];
                String mat = arr[1].split("=")[0];
                String sel = arr[1].split("=")[1];
                for(int j=0;j<matchList.size();j++){
                    Map map =(Map)matchList.get(j);
                    if(mat.equals(map.get("itemid").toString())){
                        String itemname = map.get("itemname").toString();
                        String host = map.get("host").toString();
                        String visit = map.get("visit").toString();
                        String lose = map.get("lose").toString();
                        Integer guan = i+1;
                        String oneC = "第"+guan+"关 "+itemname;
                        if(play.equals("SPF")){
                            oneC = oneC +" 胜平负";
                        }else if(play.equals("RQSPF")){
                            oneC = oneC +" 让球胜平负 让球"+lose;
                        }else if(play.equals("JQS")){
                            oneC = oneC +" 进球数";
                        }else if(play.equals("BQC")) {
                            oneC = oneC + "半全场";
                        } else if(play.equals("CBF")){
                            oneC = oneC +"比分";
                        }
                        list.add(oneC);
                        list.add("主队："+host+" VS 客队："+visit);
                        list.add(tz(play,sel));
                    }
                }
            }
        }
        return  list;
    }

    public String tz(String play,String sel){
        String[] jqArr = new String[]{"0","1","2","3","4","5","6","7+"};

        if(play.equals("SPF")||play.equals("RQSPF")){
            sel = sel.replace("/","+");
            sel = sel.replace("3","胜").replace("1","平").replace("0","负");
        }
        if(play.equals("BQC")){
            sel = sel.replace("3","胜").replace("1","平").replace("0","负");
        }
        if(play.equals("CBF")){
            sel = sel.replace("90","胜其他").replace("99","平其他").replace("09","负其他");
        }
        return sel;

    }
}
